<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Nav;

class Hsnav extends AdminBase
{

    protected function _initialize()
    {
        parent::_initialize();
        $navcat_list = [1 =>'网站导航'];
        $this->assign('navcat_list',$navcat_list);
        $m = new Nav;
        $nav_cid = input('nav_cid',1);
        $where['nav_cid'] = $nav_cid;
        $nav_list               = $m->order(['sort' => 'desc', 'id' => 'ASC'])->where($where)->select();
        $nav_level_list         = array2level($nav_list);
        $this->assign('nav_level_list',$nav_level_list);
        $this->assign('nav_cid',$nav_cid);
    }

    public function index()
    {
        return $this->fetch('index');
    }

    public function edit()
    {
        return $this->fetch('edit', ['nav' => Nav::get(input('id/d')),'pid' => input('pid/d'),'nav_cid' => input('nav_cid/d')]);
    }

    public function save()
    {
        if ($this->request->isAjax()) {
            $m = new Nav;
            return $m->_save();
        }
    }

    public function setStatus($id)
    {
        if ($this->request->isAjax()) {
            $m = new Nav();
            if ($m->allowField('status,sort')->save(input('post.'),['id'=>$id]) !== false) {
                $this->success('更新成功');
            } else {
                $this->error($m->getError());
            }
        }
    }

    public function delete($id)
    {
        if ($this->request->isAjax()) {
            $m = new Nav();
            return $m->_delete($id);
        }
    }
}